<?php
namespace App\Admin\Controllers;
use App\Admin\Models\Version;
use Common\ControllerAdBase;
use Phalcon\Paginator\Adapter\Model as PaginatorModel;
class VersionController extends ControllerAdBase
{

    public function initialize()
    {
        parent::initialize();
    }

    public function listAction(){
        //当前页数
        if(!isset($this->params['page']) || empty($this->params['page'])){
            $page = 1;
        }else{
            $page = (int)$this->params['page'];
        }
        //当前条数
        if(!isset($this->params['limit']) || empty($this->params['limit'])){
            $pageSize = 10;
        }else{
            $pageSize = (int)$this->params['limit'];
        }
        $version = new Version();
        $list = $version->version_list($this->params);
        $paginator = new PaginatorModel(
            array(
                "data" => $list,
                "limit"=> $pageSize,
                "page" => $page
            )
        );
        $list_items = $paginator->getPaginate();
        $this->ok($list_items);
    }



    public function addversionAction(){
        $this->validata_filed($this->params);
        $notices_detail['iosurl'] = $this->params['iosurl'];
        $notices_detail['description'] = $this->params['description'];
        $notices_detail['iosversion'] = $this->params['iosversion'];
        $notices_detail['is_force'] = $this->params['is_force'];
        $notices_detail['platform'] = ($this->params['platform'] == 1) ? 'ios' : 'android';
        $notices_detail['title'] = $this->params['title'];
        $notices_detail['created_at'] = date('Y-m-d H:i:s',time());
        $version = new Version();
        $result = $version->version_save($notices_detail);
        if($result){
            $this->ok([]);
        }else{
            $this->fail(-1,'添加失败');
        }
    }


    public function updateversionAction(){
        if(!isset($this->params['id']) || empty($this->params['id'])){
            $this->fail(-1,'版本id不能为空');
        }
        $this->validata_filed($this->params);
        $version = new Version();
        $notices_detail = $version->version_detail($this->params['id']);
        if(empty($notices_detail)){
            $this->fail(-1,'该公告不存在或以被删除');
        }
        $notices_detail['url'] = $this->params['iosurl'];
        $notices_detail['description'] = $this->params['description'];
        $notices_detail['version'] = $this->params['iosversion'];
        $notices_detail['is_force'] = $this->params['is_force'];
        $notices_detail['platform'] = ($this->params['platform'] == 1) ? 'ios' : 'android';
        $notices_detail['title'] = $this->params['title'];
        $notices_detail['created_at'] = date('Y-m-d H:i:s',time());
        $result = $version->version_save($notices_detail);
        if($result){
            $this->ok([]);
        }else{
            $this->fail(-1,'修改失败');
        }
    }


    private function validata_filed($params){
        if(empty($params['platform'])){
            $this->fail(-1,'更新平台不能为空');
        }
        if(empty($params['iosurl'])){
            $this->fail(-1,'下载地址不能为空');
        }
        if(empty($params['title'])){
            $this->fail(-1,'标题不能为空');
        }
        if(empty($params['description'])){
            $this->fail(-1,'简介不能为空');
        }
        if(empty($params['iosversion'])){
            $this->fail(-1,'版本号不能为空');
        }
    }


    public function delversionAction(){
        if(!isset($this->params['id']) || empty($this->params['id'])){
            $this->fail(-1,'版本id不能为空');
        }
        $version = new Version();
        $notices_detail = $version->findFirstById($this->params['id']);
        if(empty($notices_detail)){
            $this->fail(-1,'版本不存在，或已被删除');
        }
        $result = $notices_detail->delete();
        if($result){
            $this->ok([]);
        }else{
            $this->fail(-1,'删除失败');
        }
    }



    public function getDateFromRange($startdate, $enddate)
    {
        $stimestamp = strtotime($startdate);
        $etimestamp = strtotime($enddate);
        // 计算日期段内有多少天
        $days = ($etimestamp - $stimestamp) / 86400;
        // 保存每天日期
        $date = array();
        for ($i = 0; $i < $days; $i++) {
            $date[] = date('Y-m-d', $stimestamp + (86400 * $i));
        }
        return $date;
    }
}
